package com.duruikang.LeaveAPI.api;

import com.duruikang.LeaveAPI.common.http.ResponseResult;
import com.duruikang.LeaveAPI.entity.Leaving;
import com.duruikang.LeaveAPI.entity.Teacher;
import com.duruikang.LeaveAPI.form.ExamineForm;
import com.duruikang.LeaveAPI.form.TokenForm;
import com.duruikang.LeaveAPI.service.ExamineService;
import com.duruikang.LeaveAPI.service.LeavingService;
import com.duruikang.LeaveAPI.service.LogicService;
import com.duruikang.LeaveAPI.service.SecurityService;
import com.duruikang.LeaveAPI.service.SortService;
import com.duruikang.LeaveAPI.service.TeacherInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 提交审批
 *
 * @author szh
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
public class ExamineApi {

    @Autowired
    private LeavingService leavingService;

    @Autowired
    private TeacherInformationService teacherService;

    @Autowired
    private ExamineService examineService;

    @Autowired
    private LogicService logicService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    SortService sortService;

    /**
     * 提交审批，保存至数据库
     *
     * @param form 审批信息
     * @return 是否成功
     */
    @PostMapping("/examineleaving")
    public ResponseResult<Boolean> examineLeaving(@RequestBody @Valid ExamineForm form) {
        //登录检查
        String checkResult = securityService.checkToken(form.getToken());
        if (checkResult != null) {
            return ResponseResult.getErrorResult(checkResult);
        }
        //获取审批结果，转换为列要求的int
        int result = sortService.getSortByName(form.getExamineResult()).getId();
        //保存审批
        boolean res1 = examineService.submitExamination(form.getLeavingId(), form.getToken(), result, form.getExamineComment());
        //获取请假单
        Leaving leaving = leavingService.getById(form.getLeavingId());
        if (leaving == null) {
            return ResponseResult.getMessageResult(false, "E-UnknownLeaving");
        }
        //设置审批结果对应的请假单状态
        int newState = logicService.getNewState(leaving.getLeavingType(), leaving.getLeavingState(), result);
        leaving.setLeavingState(newState);
        //保存请假单
        boolean res2 = leavingService.saveOrUpdate(leaving);

        if (res1 && res2) {
            return ResponseResult.getSuccessResult(true);
        } else {
            return ResponseResult.getMessageResult(false, "E-Database");
        }
    }

    /**
     * 获取审批历史
     *
     * @param form 仅token
     * @return 6列的map list:[leavingId, leavingCode, leavingDate, examineResult, examineDate]
     */
    @RequestMapping("/getexaminehistory")
    public ResponseResult<List<Map<String, String>>> getExamineHistory(@RequestBody @Valid TokenForm form) {
        //登录检查
        String checkResult = securityService.checkToken(form.getToken());
        if (checkResult != null) {
            return ResponseResult.getErrorResult(checkResult);
        }
        //使用token获取历史
        List<Map<String, String>> res = examineService.getHistory(form.getToken());
        return ResponseResult.getSuccessResult(res);
    }

}
